/**
 * gwt-validator 
 *
 * Copyright (C) 2006  Nikolaus Rumm, Christian Simonutti
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package gwt.validator.client.action;

import gwt.validator.client.exception.ValidationIssue;

/**
 * Special action that suppresses the execution of the wrapped action in case
 * that the given condition is met.
 * 
 * Use this for defining dependencies between actions, i.e. <quote>only the
 * first occurence of action x should be executed and all following invocations
 * should be suppressed</quote>.
 * 
 * @author <a href="mailto:nikolaus.rumm@gmail.com">Nikolaus Rumm</a>
 */
public class ConditionalInvokerAction extends BaseAction implements Action {

	/**
	 * The wrapped action whose invocation will be suppressed.
	 */
	private Action action;

	/**
	 * The condition that has to be met in order to delegate the action call to
	 * the wrapped action.
	 */
	private Condition condition;

	/**
	 * Constructor
	 * 
	 * @param aAction
	 *            The action whose invocation will be suppressed
	 * @param aCondition
	 *            The condition whose evaluation result will potentially
	 *            suppress the invocation of the wrapped action
	 */
	public ConditionalInvokerAction(Action aAction, Condition aCondition) {
		super();
		this.action = aAction;
		this.condition = aCondition;
	}

	/**
	 * @see gwt.validator.client.action.Action#onFailure(gwt.validator.client.exception.ValidationIssue)
	 */
	public void onFailure(ValidationIssue aIssue) {
		if (this.condition.isTrue()) {
			this.action.onFailure(aIssue);
			this.condition.actionPerformed();
		} else {
			// suppress the execution of thw wrapped action
		}
	}

	/**
	 * @see gwt.validator.client.action.Action#onSuccess()
	 */
	public void onSuccess() {
		if (this.condition.isTrue()) {
			this.action.onSuccess();
		} else {
			// suppress the execution of thw wrapped action
		}
	}
}
